package com.woniuxy.factory.service.imp;

import com.woniuxy.factory.entity.ApplicationRecord;
import com.woniuxy.factory.entity.MateralDetailed;
import com.woniuxy.factory.entity.MaterialOut;
import com.woniuxy.factory.entity.ResponseResult;
import com.woniuxy.factory.mapper.ApplicationRecordMapper;
import com.woniuxy.factory.mapper.InventoryMapper;
import com.woniuxy.factory.mapper.MateralDetailedMapper;
import com.woniuxy.factory.mapper.MaterialOutMapper;
import com.woniuxy.factory.service.MaterialOutService;
import com.woniuxy.factory.util.ApplicationType;
import com.woniuxy.factory.util.AuditType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;

/**
 * @author 邹佳骏
 * @date 2020-08-20 20:38
 */
@Service
public class MaterialOutServiceImp implements MaterialOutService {

    @Autowired
    MaterialOutMapper materialOutMapper;

    /*注入申请类型表mapper*/
    @Autowired
    ApplicationRecordMapper applicationRecordMapper;

    @Autowired
    MateralDetailedMapper materalDetailedMapper;

    @Autowired
    InventoryMapper inventoryMapper;

    @Override
    public List<MaterialOut> selectAllMaterialOut(int page, int limit
        ,MaterialOut materialOut,String matId,String searchTime) {

        //获取起始时间和结束时间
        String startTime =null ;
        String endTime  = null;
        //非空验证
        if(searchTime!=null&&searchTime!=""){
            startTime = searchTime.substring(0,10);
            endTime = searchTime.substring(13,23);
            System.out.println("startTime:"+startTime);
            System.out.println("endTime:"+endTime);
        }

        //调用插入物料出库的方法
        insertMaterialOut();
        //查询到所有的用料申请并传递给前端
        int home = (page-1)*limit;
        List<MaterialOut> materialOuts = materialOutMapper.selectAllMaterialOut(home, limit,materialOut,matId,startTime,endTime);
        return materialOuts;
    }

    //开启事务
    @Transactional
    @Override
    public ResponseResult updatMaterialOut(MaterialOut materialOut,String matId) {
        ResponseResult responseResult = new ResponseResult<>();

        //插入修改时间
        materialOut.setMaterialouttime(new Date());
        //更改状态
        materialOut.setMaterialoutstatus(AuditType.ALREADY_PASSED);
        try {
            materialOutMapper.updateMaterialOut(materialOut,matId);
            //调用减少库存的方法更新库存
            reduceInventory(matId);
        }catch (RuntimeException e){
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            responseResult.setMsg("出库异常，请检查输入是否有错误");
        }
        responseResult.setCode(AuditType.No_Approval);
        responseResult.setMsg("出库成功");
        return responseResult;
    }
    
    
    
    @Override
    public int selectAllCount() {
        int sum = materialOutMapper.selectAllCount();
        return sum;
    }

    //插入用出库记录表的方法
    public void insertMaterialOut(){
        //创建申请类型记录表对象
        ApplicationRecord applicationRecord = new ApplicationRecord();
        //为申请表类型对象赋上用料申请的值
        applicationRecord.setApplicationtype(ApplicationType.MATERIAL_REQUISITION);
        //赋上状态为审批通过的值
        applicationRecord.setApplystatus(AuditType.ALREADY_PASSED);
        //查询到所有的用料申请表并且审核为通过的信息
        List<ApplicationRecord> applicationRecords =
                applicationRecordMapper.selectApplicationRecordByCondition(applicationRecord);
        //非空判断
        if(applicationRecords.size()>0&&!applicationRecords.isEmpty()) {
            //遍历取出用料申请编号并插入到出库表中
            for (ApplicationRecord record : applicationRecords) {
                //判断当前用料申请编号是否在出库表中存在
                int count = materialOutMapper.selectCountByMarId(record.getApplicationnum());
                if (count==0) {
                    //不存在就将这条数据插入到出库表中
                    materialOutMapper.insertMateriaOut(record.getApplicationnum());
                }
            }
        }
    }

    //减少库存的方法
    public  void reduceInventory(String matId){
        //根据用料申请编号查询所有的用料项
        List<MateralDetailed> materalDetaileds = materalDetailedMapper.selectMaterlDetailedByMatId(matId);
        for (MateralDetailed materalDetailed : materalDetaileds) {
            //获得当前的物料编号
            String maeId = materalDetailed.getMaterial().getMaeId();
            //获得当前物料的数量
            Integer number = materalDetailed.getNumber()*(-1);
            //调用仓库mapper方法更新数量
            inventoryMapper.updateInventoryAmount(number,matId);
        }

    }
}
